{% extends "base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load dv_filters %}
{% block head_title %}{% trans "Établissements" %}{% endblock %}

{% block content %}
    {% if filter.form and filter.form.address_canton %}
        <div class="modal fade bs-orga-filters-modal" tabindex="-1" role="dialog" aria-labelledby="filters">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="{% trans "Fermer" %}"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">{% trans "Filtrer la liste des établissements" %}</h4>
                    </div>
                    <div class="modal-body">
                    <form method="GET" action="." class="form-inline">
                        {% bootstrap_form filter.form size='small' exclude='q' show_help=False %}

                        <div class="modal-footer">
                            {% buttons %}
                                <a href="{% url 'organization-list' %}" class="btn btn-default">{% trans 'Reset' %}</a>
                                <button type="submit" class="btn btn-primary">{% trans 'Filter' %}</button>
                            {% endbuttons %}
                        </div>
                    </form>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}

    {% if request.user|can:"orga_detail_all" %}
        <div class="btn-group" role="group" aria-label="exports">
            {% if request.user|can:"user_view_list" %}
                {% include 'export_buttons.html' with exporturl='organization-list-export' filter_querystring=filter_querystring only %}
            {% endif %}
        </div>
    {% endif %}

    {% if filter.form %}
        {% if filter.form.address_canton %}
            <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target=".bs-orga-filters-modal">
                <span class="glyphicon glyphicon-filter" aria-hidden="true"></span>
                {% trans "Filtres" %}
            </button>
        {% endif %}

        <form method="GET" action="." class="form-inline pull-right col-xs-2">
            <div class="input-group">
                {% bootstrap_field filter.form.q show_label=False show_help=False bound_css_class='' addon_after='<span class="glyphicon glyphicon-search"></span>' %}
            </div>
        </form>
    {% endif %}

    <table class="table">
    <thead>
        <tr>
            <th>{% trans "Statut" %}</th>
            <th><abbr title="{% trans "Abréviation" %}">{% trans "Abr." %}</abbr></th>
            <th>{% trans "Nom" %}</th>
            <th>{% trans "Ville" %}</th>
            <th>{% trans "Canton" %}</th>
            <th>{% trans "Coordina·teur·trice" %}</th>
            <th>{% trans "Natel" %}</th>
            <th>{% trans "Email" %}</th>
            <th>{# Boutons #}</th>
        </tr>
    </thead>
    <tbody>
    {% for orga in organizations %}
        <tr>
            <td class="{{ orga.status_class }}">
                {{ orga.status_icon|safe }}
            </td>
            <td>{% if orga.abbr %}
                <a href="{% url 'organization-detail' pk=orga.pk %}">{{ orga.abbr_verb }}</a>
                {% endif %}</td>
            <td><a href="{% url 'organization-detail' pk=orga.pk %}">{{ orga.name }}</a></td>
            <td>{{ orga.address_zip }} {{ orga.address_city }}</td>
            <td>{{ orga.address_canton|canton_abbr }}</td>
            <td>{{ orga.coordinator.get_full_name }}</td>
            <td>{% if orga.coordinator.profile.natel %}
                {{ orga.coordinator.profile.natel|tel_link }}
                {% endif %}</td>
            <td>{% if orga.coordinator.email %}
                <a class="email"
                  href="mailto:{{ orga.coordinator.profile.mailtolink|urlencode }}">{{ orga.coordinator.email }}</a>
                {% endif %}</td>
            <td>
                {% if request.user|can:'orga_crud' and request.user|inusercantons:orga.address_canton %}
                    <a class="btn btn-primary btn-xs" href="{% url 'organization-update' pk=orga.pk %}">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                        {% trans "Éditer" %}
                    </a>
                {% endif %}
            </td>
        </tr>
    {% endfor %}
    </tbody>
    </table>

    {% include 'paginator.html' with page_obj=page_obj listurl='organization-list' filter_querystring=filter_querystring %}

    <a class="btn btn-primary pull-right" href="{% url 'organization-create' %}">{% trans "Créer un nouvel établissement" %}</a>
{% endblock %}
